<link  href="__PLU__/bootstrap-validator/bootstrapValidator.min.css" rel="stylesheet">
<link  href="__PLU__/magic-check/css/magic-check.min.css" rel="stylesheet">
<link  href="__PLU__/bootstrap-select/bootstrap-select.min.css" rel="stylesheet">
<script src="__PLU__/bootstrap-validator/bootstrapValidator.min.js"></script>
<script src="__PLU__/bootstrap-select/bootstrap-select.min.js"></script>
<script src="__PLU__/bootbox/bootbox.min.js"></script>
<div class="modal fade" id="categories-modal" role="dialog" tabindex="-1" aria-labelledby="categories-modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <!--Modal header-->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><i class="pci-cross pci-circle"></i></button>
                <h4 class="modal-title">添加分类</h4>
            </div>
            <!--Modal body-->
            <div class="modal-body">
                <form id="categories-form" class="form-horizontal" action="{:url('api/projects/add')}" method="post">
                    <div class="tab-content">
                        <div class="form-group">
                            <label class="col-lg-offset-1 col-lg-2 control-label" for="project-pname">所属项目</label>
                            <div class="col-lg-6">
                                <input type="text" class="form-control" id="project-pname" name="project[pname]" readonly="readonly">
                                <input type="hidden" id="project-pid" name="project[pid]" value="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-offset-1 col-lg-2 control-label" for="project-name">分类名称</label>
                            <div class="col-lg-6">
                                <input type="text" class="form-control" id="project-name" name="project[name]">
                                <input type="hidden" id="project-id" name="project[id]" value="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-offset-1 col-lg-2 control-label" for="project-intro">分类描述</label>
                            <div class="col-lg-6">
                                <textarea id="project-intro" name="project[intro]" rows="4" class="form-control"></textarea>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <!--Modal footer-->
            <div class="modal-footer">
                <button data-dismiss="modal" class="btn btn-default" type="button">关闭</button>
                <button class="btn btn-primary" form="categories-form" type="button">保存</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
$(document).ready(function(){
    $('#categories-form').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon',
            invalid: 'glyphicon',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            'project[name]': {
                validators: {
                    notEmpty: {
                        message: '请输入分类名称'
                    }
                }
            },
            'project[intro]': {
                validators: {
                    notEmpty: {
                        message: '请输入分类描述'
                    }
                }
            },
        }
    });
});

$('#categories-modal').on('show.bs.modal', function (e) {
    var catid = $($(e)[0].relatedTarget).data('catid');
    $('#categories-form').bootstrapValidator('resetForm', true);
    $('#project-pid').val('');
    $('#project-id').val('');
    $.ajax({
        url:"{:url('api/projects/get')}",
        type:"post",
        data:{'catid':catid},
        dataType:"json",
        success:function(result){
            if(result.status == 'success'){ 
                var project = result.data.project;
                $('#project-pname').val(project.classifyname);
                $('#project-pid').val(project.id);
                if(result.data.category){
                    var category = result.data.category;
                    $('#project-intro').val(category.description);
                    $('#project-name').val(category.classifyname);
                    $('#project-id').val(category.id);
                }
            }else{
                $.niftyNoty({
                    type: 'warning',
                    icon : 'pli-exclamation icon-2x',
                    message : result.msg,
                    container : 'floating',
                    timer : 1500,
                });
            }
        },
        error:function(){
            $.niftyNoty({
                type: 'danger',
                icon : 'pli-exclamation icon-2x',
                message : '网络连接失败',
                container : 'floating',
                timer : 1500,
            });
        }
    });
})
$('button[form="categories-form"]').on('click',function(){
    $('#categories-form').data('bootstrapValidator').validate();  
    if(!$('#categories-form').data('bootstrapValidator').isValid()){  
        return false;
    }
    $.ajax({
        url:"{:url('api/projects/add')}",
        type:"post",
        data:$('#categories-form').serialize(),
        dataType:"json",
        success:function(result){
            if(result.status == 'success'){
                $('#categories-modal').modal('hide');
                $.niftyNoty({
                    type: 'success',
                    icon : 'pli-exclamation icon-2x',
                    message : result.msg,
                    container : 'floating',
                    timer : 1500,
                });
                setTimeout(function(){
                    window.location.reload();
                },1500);
            }else{
                $.niftyNoty({
                    type: 'warning',
                    icon : 'pli-exclamation icon-2x',
                    message : result.msg,
                    container : 'floating',
                    timer : 1500,
                });
            }
        },
        error:function(){
            $.niftyNoty({
                type: 'danger',
                icon : 'pli-exclamation icon-2x',
                message : '网络连接失败',
                container : 'floating',
                timer : 1500,
            });
        }
    });
});
</script>